package com.lt.flashcard.model.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.Transient;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.LearningService;
import com.lt.flashcard.model.service.Repository;

/**
 * Define the information of a journey
 * @author ocsen
 *
 */
@Entity("journey")
public class Journey {
	
	@Id
	private ObjectId id;
	/**
	 * Store the name of this journey
	 */
	@Indexed(unique=true)
	private String name;
	/**
	 * The image for name to display in the journey list
	 */
	private String nameThumbnail = "";
	/**
	 * The date that admin define this journey
	 */
	private Date crated = new Date();
	/**
	 * The description about this journey
	 */
	private String description = "";
	/**
	 * The default background to display at the top of dashboard page
	 */
	private String dashboardBackground = "";
	/**
	 * The background to display at the left column
	 */
	private String profileBackground = "";
	/**
	 * The large thumbnail display at left column
	 */
	private String largeThumbnail = "";
	/**
	 * The small thumbnail display at right column
	 */
	private String smallThumbnail = "";
	/**
	 * Total coins that user can get with this journey
	 */
	private int totalcoin = 0;
	/**
	 * The order of this journey, user need got from lower first
	 */
	private int order = 0;
	/**
	 * The status of this journey
	 */
	private int status = STATUS_VALID;
	public static final int STATUS_VALID = 1;
	public static final int STATUS_INVALID = 0;
	/**
	 * The items in this journey
	 */
	private List<JourneyItem> items = new ArrayList<JourneyItem>();
	@Transient
	private JourneyHistory history; //Store the history for a specific user
	
	public ObjectId getId() {
		return id;
	}
	
	public void setId(ObjectId id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Date getCrated() {
		return crated;
	}
	
	public void setCrated(Date crated) {
		this.crated = crated;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public String getDashboardBackground() {
		return dashboardBackground;
	}
	
	public void setDashboardBackground(String dashboardBackground) {
		this.dashboardBackground = dashboardBackground;
	}
	
	public String getProfileBackground() {
		return profileBackground;
	}
	
	public void setProfileBackground(String profileBackground) {
		this.profileBackground = profileBackground;
	}
	
	public String getLargeThumbnail() {
		return largeThumbnail;
	}
	
	public void setLargeThumbnail(String largeThumbnail) {
		this.largeThumbnail = largeThumbnail;
	}
	
	public String getSmallThumbnail() {
		return smallThumbnail;
	}
	
	public void setSmallThumbnail(String smallThumbnail) {
		this.smallThumbnail = smallThumbnail;
	}
	
	public int getTotalcoin() {
		return totalcoin;
	}
	
	public void setTotalcoin(int totalcoin) {
		this.totalcoin = totalcoin;
	}
	
	public List<JourneyItem> getItems() {
		return items;
	}
	
	public void setItems(List<JourneyItem> items) {
		this.items = items;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getNameThumbnail() {
		return nameThumbnail;
	}

	public void setNameThumbnail(String nameThumbnail) {
		this.nameThumbnail = nameThumbnail;
	}

	public JourneyHistory getHistory() {
		if(history == null) {
			LearningService srv = Repository.getInstance().getLearningService();
			AAService asrv = Repository.getInstance().getAAService();
			User u = asrv.getCurrentUser();
			if(u == null)
				return null;
			
			history = srv.getJourneyHistoryOfUser(u.getId(), this);
		}
		return history;
	}
	
	public JourneyHistory getHistory(User user) {
		if(history == null) {
			LearningService srv = Repository.getInstance().getLearningService();
			if(user == null)
				return null;
			
			history = srv.getJourneyHistoryOfUser(user.getId(), this);
		}
		return history;
	}

	public void setHistory(JourneyHistory history) {
		this.history = history;
	}
	
	public void clearHistory() {
		this.history = null;
	}
}
